<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
       $this->display();
    }
    
    public function top(){
    	$user_name = session("user_name");
    	$model = M("sys_user");
    	$data = $model->where("user_name='%s'",$user_name)->find();
    	$data["dept_name"] = M("sys_dept")->where("id=%d", session("dept_id"))->getField("dept_name");
    	$this->assign("data", $data);
    	$path = C("PUBLIC_PATH")."avatar";
		$filename = $_SERVER['DOCUMENT_ROOT'] . $path . "/$user_name.jpg";
    	if(!is_file($filename)){
    		$photo = "default.jpg";
    	}else{
    		$photo = "$user_name.jpg";
    	}
    	$this->assign("photo", $photo);
   		$this->display();
    }
    
    public function menu(){
    	$user_name = session("user_name");
    	$data_user = M("sys_user")->where("user_name = '%s'",$user_name)->find();
    	$priv = $data_user['priv_id'].','.$data_user['priv_aid_id'];
    	$priv = explode(",", trim($priv, ","));
    	foreach ($priv as $value){
    		$where .= "id = ".$value." or ";
    	}
    	$where = substr($where, 0, -4);
    	$sql = "SELECT purview FROM oa_sys_priv WHERE ".$where;
    	$purview = M()->query($sql);
    	foreach ($purview as $value){
    		if( !empty($value['purview']) ){
    			$menu_id .= $value['purview'].",";
    		}
    	}
    	$menu = array_unique( explode(",", trim($menu_id, ",")) );
    	session("menu",$menu);
    	$where = '';
    	foreach ($menu as $value){
    		$where .= "menu_id = ".$value." or ";
    	}
    	$where = substr($where, 0, -4);
    	$sql = "SELECT menu_id, menu_name,action FROM oa_sys_menu WHERE leve = 0 and status = 1 and pid = 0 and (".$where.")";
    	$data = M()->query($sql);
    	$this->assign("data", $data);
   		$this->display();
    }
    
    
    
    public function main(){
		$user_name = session('user_name');
		$model = M();
    	//公告
    	$circular = M("circular");
		$data_circular = $circular->order('create_time desc')->limit('0','5')->select();
		$this->assign('data_circular',$data_circular);
		
		//待办事项
		$sql = "select a.prcs_id, c.proc_id, a.prcs_flag, d.work_id,d.work_name, d.type_id from oa_workflow_work_prcs a, oa_workflow_work d, oa_workflow_process c where d.work_status=1 and a.work_id=d.work_id and d.type_id=c.type_id and a.proc_no=c.proc_no and a.proc_user='$user_name' and a.proc_no IN (select MAX(proc_no) FROM oa_workflow_work_prcs b WHERE b.work_id=a.work_id ) order by d.work_time desc limit 0,5";
		$data_work = $model->query($sql);
		$this->assign("data_work",$data_work);
		
		//今日指纹考勤
		$sql = "select c.* from oa_sys_user u, oa_checkinout c where u.user_name='$user_name' and u.user_id=c.user_id and to_days(c.checktime)=(to_days(now())) order by c.checktime desc";
		$data = $model->query($sql);
		if( count($data) > 0){
			$data_checkinout['start'] = $data[count($data)-1]['checktime'];
			$data_checkinout['end'] = $data[0]['checktime'];
		}else{
			$data_checkinout['start'] = $data_checkinout['end'] = "暂无数据！";
		}
		$this->assign("data_checkinout",$data_checkinout);
//		dump($data_checkinout);
		
   		$this->display();
    }
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>